package com.desire.test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author desire
 */
public class _30_缩略图_改变图像尺寸 {
    public static void main(String[] args) throws IOException {
        var src = ImageIO.read(new File("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\bagua.png"));
        var sw = src.getWidth();
        var sh = src.getHeight();

        var dw = 120;
        var dh = 120;
        var dst = new BufferedImage(dw, dh, 1);
        var g = dst.createGraphics();
        //缩略图 0,0 dw,dh 目标宽高
        //g.drawImage(src, 0, 0, dw, dh, null);
        //g.drawImage(src, 0, 0, dw, dh, 0, 0, sw, sh, null);

        //剪切图像 0,0 sw,sh 源图像宽高
        int x = 130;
        int y = 10;
        //g.drawImage(src, -x, -y, sw, sh, null);
        //裁剪图像，src 代码要裁剪的图像  0 0 dw dh 目标图像尺寸
        //x,y,x+dw,y+dh 被裁剪的图像的位置尺寸，null父依赖对象swing，
        g.drawImage(src, 0, 0, dw, dh, x, y, x + dw, y + dh, null);
        dst = rotateImage(dst, 90);
        //释放占用的内存
        g.dispose();
        //输出dst缓存图像到文件中,jpg是格式 gif png jpg
        ImageIO.write(dst, "png", new File("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\bagua_旋转.png"));

        System.out.println(sw);
        System.out.println(sh);
    }

    /**
     * 变更图像为指定大小
     *
     * @param bufferedimage 目标图像
     * @param w             宽
     * @param h             高
     * @return
     */
    public static BufferedImage resizeImage(final BufferedImage bufferedimage,
                                            final int w, final int h) {
        int type = bufferedimage.getColorModel().getTransparency();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = create(w, h, type))
                .createGraphics()).setRenderingHint(
                RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.drawImage(bufferedimage, 0, 0, w, h, 0, 0, bufferedimage
                .getWidth(), bufferedimage.getHeight(), null);
        graphics2d.dispose();
        return img;
    }

    public static BufferedImage create(int w, int h, int type) {
        return new BufferedImage(w, h, type);
    }

    /**
     * 旋转图片为指定的角度
     *
     * @param bufferedImage 传入的源图像
     * @param degree        角度
     * @return 返回一张旋转后的图片
     */
    public static BufferedImage rotateImage(final BufferedImage bufferedImage, final int degree) {
        // 获得 长 宽 颜色透明度
        int w = bufferedImage.getWidth();
        int h = bufferedImage.getHeight();
        int type = bufferedImage.getColorModel().getTransparency();
        BufferedImage img;
        Graphics2D graphics2d;

        (graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        // 以正角度旋转使正x轴上的点旋转到正y轴上。 参数: -以弧度为单位的旋转角度 X -旋转原点的X坐标 Y -旋转原点的Y坐标
        graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
        graphics2d.drawImage(bufferedImage, 0, 0, null);
        graphics2d.dispose();
        return img;
    }

    public static void main1(String[] args) throws IOException {
        String p = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\bagua.png";
        BufferedImage src = ImageIO.read(new File(p));
        int sw = src.getWidth();
        int sh = src.getHeight();

        int dw = 250;
        int dh = (int) (sh * (1.0 * dw / sw));
        BufferedImage dst = new BufferedImage(dw, dh, 1);
        Graphics2D g = dst.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        g.drawImage(src, 0, 0, dw, dh, null);
        g.dispose();
        ImageIO.write(dst, "png", new File("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\bagua_suolve.png"));
        Runtime run = Runtime.getRuntime();

        run.exec("cmd /k start C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\bagua_suolve.png");
    }
}
